
const apiUrl = process.env.VUE_APP_BRACELET_API
const baseRequest = (url, method, params, contentType) => {
    console.log(uni.getStorageSync('access_token'))
    const token = uni.getStorageSync('access_token')
    const refreshToken = uni.getStorageSync('refreshToken')

    return new Promise((resolve, reject) => {
        uni.request({
            url: apiUrl + url, // 测试接口
            method: method,
            data: params,
            header: {
                'Content-Type': contentType || 'application/json',
                // 'access_token': token,
                'Authorization': 'Bearer ' + token
            }
        }).then((response) => {
            console.log(response, 'response')
            const res = response
            if (res.data.accessTokenExpired === "true" || res.data.accessTokenExpired === true || res.statusCode === 401 || res.data.code === -1) {
               
                uni.request({
                    url: apiUrl + 'api/miniProgram/refresh',
                    method: 'POST',
                    // data: newApiData,
                    header: {
                        'Content-Type': contentType || 'application/json',
                        'Authorization': refreshToken
                    }
                }).then((newResponse) => {
                    console.log(newResponse, 'newResponse')
                    const newRes = newResponse
                    if (newRes.statusCode === 401 || newRes.data.code === -1) {
                        uni.navigateTo({
                            url: '/pages/login/loginSelect'
                        })
                    } else {
                        const access_token = newRes.data.token
                        const refreshToken = newRes.data.refreshToken
                        uni.setStorage({
                            key: 'access_token',
                            data: access_token
                        })
                        uni.setStorage({
                            key: 'refreshToken',
                            data: refreshToken
                        })
                        uni.request({
                            url: apiUrl + url, // 测试接口
                            method: method,
                            data: params,
                            header: {
                                'Content-Type': contentType || 'application/json',
                                'Authorization': 'Bearer ' + access_token
                            }
                        }).then((response1) => {
                            console.log(response1, 'response1')
                            const res = response1
                            resolve(res.data)
                        }).catch(error => {
                            console.log(error, 'error')
                            // 弹出消息提示
                            uni.showToast({
                                icon: 'none', // 不显示图标，最多显示2行
                                title: '服务器错误！',
                                duration: 2000
                            })

                            const err = error[0]

                            reject(err)
                        })
                        resolve(newRes.data)
                    }

                }).catch((newError) => {
                    console.log(newError);
                    const newErr = newError[0]
                    reject(newErr);
                });
            } else if (res.data.refreshExpiredCode === true) {
                // 接口出问题，可能是token失效了，返回登陆页
                uni.clearStorage()
                uni.navigateTo({
                    url: '/pages/login/loginSelect'
                })
            }
            // else if(res.statusCode === 401 || res.data.code === -1) {
            //     // 接口出问题，可能是token失效了，返回登陆页
            //     // uni.clearStorage()
            //     uni.navigateTo({
            //         url: '/pages/login/loginSelect'
            //     })
            // } 
            else {
                resolve(res.data)
            }
        }).catch(error => {
            console.log(error, 'error')
            // 弹出消息提示
            uni.showToast({
                icon: 'none', // 不显示图标，最多显示2行
                title: '服务器错误！',
                duration: 2000
            })

            const err = error[0]

            reject(err)
        })
    })
}

export default {
    baseRequest
}
